package com.dyrj.pig.service.impl;

import com.alibaba.fastjson.JSON;
import com.dyrj.jzb.po.Po6007MembershipTrancheFreeze;
import com.dyrj.jzb.service.JzbApiService;
import com.dyrj.pig.bean.TJzbFreeze;
import com.dyrj.pig.dao.TJzbFreezeMapper;
import com.dyrj.pig.service.ITJzbFreezeService;
import com.dyrj.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dyrj.base.system.user.model.SysUser;
import com.dyrj.component.properties.UploadProperties;
import com.dyrj.constant.SysFrameConst;
import com.dyrj.utils.UUIDUtil;
import com.dyrj.vo.PageResultMsg;
import com.dyrj.vo.ResultMsg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 见证宝--冻结解冻日志信息 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-10-31
 */
@Service
@Log4j2
public class TJzbFreezeServiceImpl implements ITJzbFreezeService {

    @Resource
    private TJzbFreezeMapper mapper;

    @Resource
    private UploadProperties uploadProperties;

    @Autowired
    private JzbApiService jzbApiService;

    @Override
    public PageResultMsg table(TJzbFreeze record) {
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(SysFrameConst.SYSTEM_USER);
        record.setUserid(loginUser.getAccount());
        PageInfo<TJzbFreeze> list = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> mapper.selectList(record));
        return new PageResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, list.getTotal(), list.getList());
    }


    @Override
    public ResultMsg add(TJzbFreeze record) {
        String msg = "";
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(SysFrameConst.SYSTEM_USER);
//        record.setId(UUIDUtil.uuid());
        record.setInsertime(new Date());
        record.setUpdatetime(new Date());
        record.setUserid(loginUser.getAccount());
        try {
            mapper.insertSelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE+msg);
    }


    @Override
    public TJzbFreeze info(String id) {
        TJzbFreeze ylMmdagl = mapper.selectByPrimaryKey(Integer.valueOf(id));
        return ylMmdagl;
    }

    public ResultMsg update(TJzbFreeze record) {
        String msg = "";
        try {
            //TJzbFreeze record_db = mapper.selectByPrimaryKey(record.getId());
            record.setUpdatetime(new Date());
            mapper.updateByPrimaryKeySelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE + msg);
    }

    public ResultMsg unfreeze(String id,String jdje){
        ResultMsg resultMsg = null;
        if(StringUtil.isEmpty(id) || StringUtil.isEmpty(jdje)){
            resultMsg = new ResultMsg(SysFrameConst.ResultCode.FAIL.CODE, "输入参数错误");
        }

        String msg = "";
        try {
            TJzbFreeze record_db = mapper.selectByPrimaryKey(Integer.valueOf(id));
            record_db = record_db == null?new TJzbFreeze():record_db;
            String subAcctNo = record_db.getSubacctno();
            String tranNetMemberCode = record_db.getTrannetmembercode();
            String tranAmt = jdje;
            String tranCommission = "0.00";
            String orderno = record_db.getOrderno();
            String orderContent="";
            String functionFlag="4";

            /*
            FunctionFlag	功能标志	string(1)	Y
            1：冻结（会员→担保）
            2：解冻（担保→会员）
            4：见证+收单的冻结资金解冻
            5: 可提现冻结（会员→担保）
            6: 可提现解冻（担保→会员）
            7: 在途充值解冻（担保→会员）
            */
            Po6007MembershipTrancheFreeze po6007MembershipTrancheFreeze  = jzbApiService.ApiPo6007MembershipTrancheFreeze(functionFlag, subAcctNo, tranNetMemberCode, tranAmt, tranCommission, orderno, orderContent);
            String code = po6007MembershipTrancheFreeze.getTxnReturnCode();

            if(!StringUtil.isEmpty(code) && "000000".equals(code)){
                resultMsg = new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, "操作成功！");

                //记录解冻成功日志，用于判断冻结金额还剩下多少可以解冻
                try{
                    Date now = new Date();
                    TJzbFreeze tJzbFreeze = new TJzbFreeze();
                    tJzbFreeze.setUserid(record_db.getUserid());
                    tJzbFreeze.setSubacctno(subAcctNo);
                    tJzbFreeze.setTrannetmembercode(tranNetMemberCode);
                    tJzbFreeze.setFunctionflag(functionFlag);
                    tJzbFreeze.setTranamt(tranAmt);
                    tJzbFreeze.setTrancommission(tranCommission);
                    tJzbFreeze.setOrderno(orderno);
                    tJzbFreeze.setOrdercontent(orderContent);
                    tJzbFreeze.setInsertime(now);
                    tJzbFreeze.setUpdatetime(now);
                    mapper.insertSelective(tJzbFreeze);
                }catch (Exception e){
                    e.printStackTrace();
                }

            }else{
                resultMsg = new ResultMsg(SysFrameConst.ResultCode.FAIL.CODE, po6007MembershipTrancheFreeze.getTxnReturnMsg());
            }
            log.debug(JSON.toJSONString(po6007MembershipTrancheFreeze));

        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
        return resultMsg;
    }

    @Override
    public ResultMsg delete(String id) {
        mapper.deleteByPrimaryKey(Integer.valueOf(id));
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE);
    }

}
